//
//  BBUser.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/13.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBUser: NSObject,NSCoding{
    @objc var id:NSNumber?
    @objc var serverId:String?
    @objc var userName:String?
    @objc var userType:NSNumber?
    @objc var email:String?
    @objc var phone:String?
    @objc var sex:NSNumber?
    @objc var headImage:String?
    @objc var createTime:NSNumber?
    @objc var updateTime:NSNumber?
    @objc var token:String?
    @objc var userId:String?
    @objc var devices:[BBDevice]?
    
    
    init(dict:[String:AnyObject]){
        super.init()
//        id = dict["id"] as? String
//        serverId = dict["serverId"] as? String
//        username = dict["userName"] as? String
//        userType = dict["userType"] as? Int
//        email = dict["email"] as? String
//        sex = dict["sex"] as? Int
//        phone = dict["phone"] as? String
//        headImage = dict["headImage"] as? String
//        createTime = dict["createTime"] as? String
//        updateTime = dict["updateTime"] as? String
//        userId = dict["userId"]as?String
//        token = dict["token"]as?String
        setValuesForKeys(dict)
    }
    
    public func resetDevices(devices:[BBDevice]){
        self.devices = devices
        saveUser()
    }
    
    public func resetUserData(dict:[String:AnyObject]){
        setValuesForKeys(dict)
        saveUser()
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print(key)
    }
    
    func saveUser(){
        NSKeyedArchiver.archiveRootObject(self, toFile: "user.plist".cacheDir())
    }
    
    class func userLogin() -> Bool {
        return BBUser.loadUser() != nil

    }
    
    static var user:BBUser?
    class func loadUser()->BBUser?{
        if user != nil {
            return user
        }
        user = NSKeyedUnarchiver.unarchiveObject(withFile: "user.plist".cacheDir()) as? BBUser
        return user;
    }
    
    class func removeUser(){
       user = nil
        do {
            try! FileManager.default.removeItem(atPath: "user.plist".cacheDir())
        } catch  {}
      
    }
    
    //将对象写入
    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(userName, forKey: "userName")
        aCoder.encode(email, forKey: "email")
        aCoder.encode(userId, forKey: "userId")
        aCoder.encode(phone, forKey: "phone")
        aCoder.encode(userType, forKey: "userType")
        aCoder.encode(serverId, forKey: "serverId")
        aCoder.encode(createTime, forKey: "createTime")
        aCoder.encode(updateTime, forKey: "updateTIme")
        aCoder.encode(headImage, forKey: "headImage")
        aCoder.encode(sex, forKey: "sex")
        aCoder.encode(token, forKey: "token")
        aCoder.encode(devices,forKey:"devices")
    }
    
    //读取对象
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id")as?NSNumber
        userName = aDecoder.decodeObject(forKey: "userName")as?String
        email = aDecoder.decodeObject(forKey: "email")as?String
        userId = aDecoder.decodeObject(forKey: "userId") as?String
        serverId = aDecoder.decodeObject(forKey: "serverId") as?String
        headImage = aDecoder.decodeObject(forKey: "headImage") as?String
        phone = aDecoder.decodeObject(forKey: "phone") as?String
        userType = aDecoder.decodeObject(forKey: "userType") as? NSNumber
        sex = aDecoder.decodeObject(forKey: "sex") as? NSNumber
        createTime = aDecoder.decodeObject(forKey: "createTime") as? NSNumber
        updateTime = aDecoder.decodeObject(forKey: "updateTime") as? NSNumber
        token = aDecoder.decodeObject(forKey: "token") as? String
        devices = aDecoder.decodeObject(forKey: "devices") as? [BBDevice]
    }
}

